define(['jquery', 'bootstrap', 'backend', 'form', 'table'], function ($, undefined, Backend, Form, Table) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'qingdongwms/order/order/index',
                    detail_url : 'qingdongwms/order/order/detail',
                    table: 'order'
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                sortName: 'id',
                columns: [
                    [
                        {field: 'state', checkbox: true},
                        {field: 'customer', title: __('客户名称'), formatter : function (value, row, index) {
                                if(!value){
                                    return '';
                                }
                                return "<a href='javascript:void(0);' data-id='" + row.customer_id + "' class='show-customer'>" + value.name + "</a>";
                            }, operate:'like'},
                        {field: 'contacts', title: __('客户联系人'), formatter : function (value, row, index) {
                                if(!value){
                                    return '';
                                }
                                return "<a href='javascript:void(0);' data-id='" + row.contacts_id + "' class='show-contact'>" + value.name + "</a>";
                            }, operate:'like'},
                        {field: 'contract', title: __('关联合同'), formatter : function (value, row, index) {
                                if(!value){
                                    return '';
                                }
                                return "<a href='javascript:void(0);' data-id='" + row.contract_id + "' class='show-contract'>" + value.num + "</a>";
                            }, operate:'like'},
                        {field: 'contract.money', title: __('合同费用'), operate:false},
                        {field : 'is_account', title : '是否已到账',operate:'=',searchList:{0:'否',1:'是'},formatter:Table.api.formatter.status},

                        {field: 'owner_staff_id', title: __('负责人'), operate:'=',searchList:$.getJSON("qingdongwms/customer/customer/stafflist")},
                        {field: 'create_staff.name', title: __('创建人'), operate:false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field     : 'operate',
                            title     : __('Operate'),
                            table     : table,
                            events    : Table.api.events.operate,
                            formatter : Table.api.formatter.operate,
                            buttons   : [
                                {
                                    name: '到账',
                                    text: __('到账'),
                                    classname : 'records btn-dialog',
                                    url: 'qingdongwms/order/order/input_payment',
                                    callback: function (data) {
                                        $('.btn-success').trigger('click');
                                    },
                                },{
                                    name: 'detail',
                                    text: __('详情'),
                                    classname: 'records btn-dialog',
                                    url: 'qingdongwms/order/order/detail',
                                    extend: "data-area='[\"80%\", \"80%\"]'",
                                    callback: function (data) {
                                        console.log(data,'data');
                                        Layer.alert("接收到回传数据：" + JSON.stringify(data), {title: "回传数据"});
                                    }
                                },

                            ]
                        }
                    ]
                ],
                //启用普通表单搜索
                commonSearch: true,
                searchFormVisible: true,
                onLoadSuccess:function(){
                    // 这里就是数据渲染结束后的回调函数
                    $(".btn-add").data("area",["80%","80%"]);
                    $(".btn-edit").data("area",["80%","80%"]);
                }
            });
            $(document).on('click', '.show-customer', function (data) {
                var area = [$(window).width() > 1200 ? '1200px' : '95%', $(window).height() > 800 ? '800px' : '95%'];
                var options = {
                    shadeClose : false,
                    shade      : [0.3, '#393D49'],
                    area       : area,
                    callback   : function (value) {
                        //在回调函数里可以调用你的业务代码实现前端的各种逻辑和效果
                        console.log(value);
                    }
                };
                Fast.api.open("qingdongwms/customer/customer/detail?ids=" + $(this).data('id'), '客户详情', options);
            });
            $(document).on('click', '.show-contact', function (data) {
                var area = [$(window).width() > 1200 ? '1200px' : '95%', $(window).height() > 800 ? '800px' : '95%'];
                var options = {
                    shadeClose : false,
                    shade      : [0.3, '#393D49'],
                    area       : area,
                    callback   : function (value) {
                        //在回调函数里可以调用你的业务代码实现前端的各种逻辑和效果
                        console.log(value);
                    }
                };
                Fast.api.open("qingdongwms/customer/contacts/detail?ids=" + $(this).data('id'), '联系人详情', options);
            });
            $(document).on('click', '.show-contract', function (data) {
                var area = [$(window).width() > 1200 ? '1200px' : '95%', $(window).height() > 800 ? '800px' : '95%'];
                var options = {
                    shadeClose : false,
                    shade      : [0.3, '#393D49'],
                    area       : area,
                    callback   : function (value) {
                        //在回调函数里可以调用你的业务代码实现前端的各种逻辑和效果
                        console.log(value);
                    }
                };
                Fast.api.open("qingdongwms/customer/contract/detail?ids=" + $(this).data('id'), '合同详情', options);
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            $('.search').hide();
            $('.btn-import').hide();

        },
        add: function () {
            Controller.api.bindevent();

        },

        edit: function () {
            Controller.api.bindevent();
        },
        input_payment: function () {
            Controller.api.bindevent();
        },
        detail : function () {
            // 初始化表格参数配置
            Table.api.init({});
            // 公司产品
            var contract_id = $('#contract_id').val()
            var order_id = $('#order_id').val()
            var tablepro = $("#table-product");
            tablepro.bootstrapTable({
                url          : 'qingdongwms/order/order/get_product?contract_id=' + contract_id,
                pk           : 'id',
                sortName     : 'id',
                operate      : false,
                commonSearch : false,
                search       : false,
                visible      : false,
                showColumns  : false,
                showExport   : false,
                showToggle   : false,
                columns      : [
                    [
                        {field: 'name', title: __('产品名称'), formatter : function (value, row, index) {
                                if(!value){
                                    return '';
                                }
                                return "<a href='javascript:void(0);' data-id='" + row.product_id + "' class='show-product'>" + row.name + "</a>";
                            }, operate:false},
                        {field : 'unit', title : '产品单位'},
                        {field : 'img', title : '产品图', formatter: Table.api.formatter.image},
                        {field : 'number', title : '数量'},
                        {field : 'price', title : '金额'},
                        {field : 'num', title : '产品编码'},
                    ]
                ]
            });
            var tablerecord = $("#table-record");
            tablerecord.bootstrapTable({
                url          : 'qingdongwms/order/order/get_record?order_id=' + order_id,
                pk           : 'id',
                sortName     : 'id',
                operate      : false,
                commonSearch : false,
                search       : false,
                visible      : false,
                showColumns  : false,
                showExport   : false,
                showToggle   : false,
                escape: false,//html标签转义
                columns      : [
                    [
                        {field : 'content', title : '到账信息'},
                        {field : 'file_ids', title : '到账凭证', formatter: Controller.api.formatter.thumb},
                    ]
                ]
            });
            // quote
            var tablejoint_follow = $("#table-joint_follow");
            tablejoint_follow.bootstrapTable({
                url          : 'qingdongwms/order/order/getShowStaff?ids=' + order_id,
                pk           : 'id',
                sortName     : 'id',
                operate      : false,
                commonSearch : false,
                search       : false,
                visible      : false,
                showColumns  : false,
                showExport   : false,
                showToggle   : false,
                columns      : [
                    [
                        {field : 'staff.img', title : '头像', formatter: Table.api.formatter.image},
                        {field : 'staff.name', title : '员工姓名'},
                        {field : 'staff.mobile', title : '员工电话'},
                        {field : 'staff.post', title : '员工角色'},
                        {field : 'roles', title : '团队角色'},
                        {field : 'is_edit', title : '权限'},
                    ]
                ]
            });
            $(document).on('click','.btn-set_team',function () {
                var id=$('#ids').val();
                Fast.api.open("qingdongwms/order/order/set_team?ids="+id, "团队修改",{
                    shadeClose: false,
                    shade: false,
                    maxmin: false,
                    moveOut: false,
                    scrollbars:false,
                    area:["90%","90%"],
                    callback:function () {
                        location.reload();
                    }
                });
            })
            Controller.api.bindevent();
        },
        set_team    : function () {
            $(document).on('click','.btn-zhidu',function () {
                $(this).parent().parent().find('.is_edit').html('只读')
                $(this).parent().parent().find('.staff_is_edit').val(0)
                $(this).parent().find('.btn-duxie').show()
                $(this).hide()
                layer.msg('权限更改为只读')
            })
            $(document).on('click','.btn-duxie',function () {
                $(this).parent().parent().find('.is_edit').html('读写')
                $(this).parent().parent().find('.staff_is_edit').val(1)
                $(this).parent().find('.btn-zhidu').show()
                $(this).hide()
                layer.msg('权限更改为读写')
            })
            $(document).on('click','.btn-delone',function () {
                $(this).parent().parent().remove()
            }).on('click','.btn-add_team',function () {
                var ids=$('#id').val();
                Fast.api.open("qingdongwms/order/order/add_team?ids="+ids, '添加团队成员',{
                    shadeClose: false,
                    shade: false,
                    maxmin: false,
                    moveOut: false,
                    scrollbars:false,
                    'area':[
                        $(window).width() > 800 ? '80%' : '80%',
                        $(window).height() > 600 ? '80%' : '80%'
                    ],
                    callback:function(value){
                        if (value.is_edit == 1){
                            var edit_html = '读写';
                        }else{
                            var edit_html = '只读';
                        }

                        var html = '<tr>\n' +
                            '           <input type="hidden" name="row[staff][{$key+1}][id]" value="'+value.id+'"/>\n' +
                            '           <input type="hidden" name="row[staff][{$key+1}][is_edit]" class="staff_is_edit" value="'+value.is_edit+'"/>\n' +
                            '           <input type="hidden" name="row[staff][{$key+1}][roles]" value="2"/>\n' +
                            '           <td class="text-center">'+value.name+'</td>\n' +
                            '           <td class="text-center">'+value.post+'</td>\n' +
                            '           <td class="text-center is_edit">'+edit_html+'</td>\n' +
                            '           <td class="text-center">\n';
                        if (value.is_edit == 1){
                            html += '              <a class="btn btn-xs btn-info btn-zhidu">只读</a>\n' +
                                '              <a class="btn btn-xs btn-info btn-duxie" style="display: none">读写</a>\n';
                        }else{
                            html += '              <a class="btn btn-xs btn-info btn-zhidu" style="display: none">只读</a>\n' +
                                '              <a class="btn btn-xs btn-info btn-duxie">读写</a>\n';
                        }

                        html += '              <a href="javascript:;" class="btn btn-xs btn-danger btn-delone"   data-original-title="删除"><i class="fa fa-trash"></i></a>\n' +
                            '           </td>\n' +
                            '        </tr>';
                        $('#table-team').append(html)
                        Form.events.plupload("#plupload-local");
                        //   在这里可以接收弹出层中使用`Fast.api.close(data)`进行回传数据
                    },
                });
            })

            Controller.api.bindevent();
        },

        add_team    : function () {
            Form.api.bindevent($("form[role=form]"), function(data, ret){
                //这里是表单提交处理成功后的回调函数，接收来自php的返回数据
                Fast.api.close(data);//这里是重点
            });
        },
        api: {
            bindevent: function () {
                $(document).on('click', '.show-product', function (data) {
                    var area = [$(window).width() > 1200 ? '1200px' : '95%', $(window).height() > 800 ? '800px' : '95%'];
                    var options = {
                        shadeClose : false,
                        shade      : [0.3, '#393D49'],
                        area       : area,
                        callback   : function (value) {
                            //在回调函数里可以调用你的业务代码实现前端的各种逻辑和效果
                            console.log(value);
                        }
                    };
                    Fast.api.open("qingdongwms/product/product/detail?ids=" + $(this).data('id'), '产品详情', options);
                });
                $(document).on('click', '.customer', function (data) {
                    var customer_id = $(this).attr('data-value')
                    var area = [$(window).width() > 1200 ? '1200px' : '95%', $(window).height() > 800 ? '800px' : '95%'];
                    var options = {
                        shadeClose : false,
                        shade      : [0.3, '#393D49'],
                        area       : area,
                        callback   : function (value) {
                            //在回调函数里可以调用你的业务代码实现前端的各种逻辑和效果
                            console.log(value);
                        }
                    };
                    Fast.api.open("qingdongwms/customer/customer/detail?ids=" + customer_id, '客户详情', options);
                });
                $(document).on('click', '.contract', function (data) {
                    var protract_id = $(this).attr('data-value')
                    var area = [$(window).width() > 1200 ? '1200px' : '95%', $(window).height() > 800 ? '800px' : '95%'];
                    var options = {
                        shadeClose : false,
                        shade      : [0.3, '#393D49'],
                        area       : area,
                        callback   : function (value) {
                            //在回调函数里可以调用你的业务代码实现前端的各种逻辑和效果
                            console.log(value);
                        }
                    };
                    Fast.api.open("qingdongwms/customer/contract/detail?ids=" + protract_id, '合同详情', options);
                });
                $(document).on('click', '.contacts', function (data) {
                    var contacts_id = $(this).attr('data-value')
                    var area = [$(window).width() > 1200 ? '1200px' : '95%', $(window).height() > 800 ? '800px' : '95%'];
                    var options = {
                        shadeClose : false,
                        shade      : [0.3, '#393D49'],
                        area       : area,
                        callback   : function (value) {
                            //在回调函数里可以调用你的业务代码实现前端的各种逻辑和效果
                            console.log(value);
                        }
                    };
                    Fast.api.open("qingdongwms/customer/contacts/detail?ids=" + contacts_id, '客户联系人详情', options);
                });
                Form.api.bindevent($("form[role=form]"), function(data, ret){
                    //这里是表单提交处理成功后的回调函数，接收来自php的返回数据
                    Fast.api.close(data);//这里是重点
                });
            },
            formatter: {
                thumb : function (value, row, index) {
                    var length = row.file_ids.length;
                    var html='';
                    for(var i=0;i<length;i++){
                        html += '<a href="' + row.file_ids[i] + '" target="_blank"><img src="' + row.file_ids[i] + '" data-tips-image alt="" title="" style="max-height:90px;max-width:120px;margin-right:10px;"></a>';

                    }
                    return html;
                },

            }
        }

    };
    return Controller;
});